﻿using EMoney.IStock.Client.DataAccess.MC;
using EMoney.IStock.Client.Framework;
using EMoney.IStock.Client.Models.MC;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Dynamic;

namespace EMoney.IStock.Client.Business.MC
{
    /// <summary>
    /// i豆等级业务逻辑类
    /// </summary>
    public class ILevelInfoBll
    {
        private object objILevelInfo = new object();

        private const string ILEVELINFO_MEMCACHED_KEY = "EMoney.IStock.Client.Business.MC.ILevelInfoBll.ILevelInfo";

        /// <summary>
        /// 获取i豆等级信息
        /// </summary>
        /// <returns></returns>
        public List<MC_ILevelInfo> GetILevelInfoList()
        {
            List<MC_ILevelInfo> list = CacheManager.RedisCache.Get<List<MC_ILevelInfo>>(ILEVELINFO_MEMCACHED_KEY);

            if (list != null && list.Count > 0)
            {
                return list;
            }
            else
            {
                try
                {
                    lock (objILevelInfo)
                    {
                        list = CacheManager.RedisCache.Get<List<MC_ILevelInfo>>(ILEVELINFO_MEMCACHED_KEY);

                        if (list == null || list.Count == 0)
                        {
                            ILevelInfoDal dal = new ILevelInfoDal();

                            list = dal.GetILevelInfoList();

                            if (list == null)
                            {
                                list = new List<MC_ILevelInfo>();
                            }

                            CacheManager.DefaultCache.Set<List<MC_ILevelInfo>>(ILEVELINFO_MEMCACHED_KEY, list);
                        }
                    }
                }
                catch (Exception ex)
                {
                    LogManager.DefaultLogger.Error(ex, "获取i豆等级信息失败");
                }

                return list;
            }
        }

        /// <summary>
        /// 获取指定i豆 等级信息
        /// </summary>
        /// <param name="iNum"></param>
        /// <returns></returns>
        public MC_ILevelInfo GetILevelInfo(int iNum)
        {
            List<MC_ILevelInfo> iLevelInfoList = GetILevelInfoList();

            if (iLevelInfoList != null && iLevelInfoList.Count > 0)
            {
                if (iNum >= 20280)
                {
                    return iLevelInfoList.Find(ilevel => ilevel.LevelID == 30); //目前最高30级
                }
                else
                {
                    iLevelInfoList = iLevelInfoList.FindAll(ilevel => ilevel.INum <= iNum); //获取指定i豆 以下的等级列表

                    if (iLevelInfoList != null && iLevelInfoList.Count > 0)
                    {
                        iLevelInfoList = iLevelInfoList.AsQueryable().OrderBy("INum desc").ToList();

                        return iLevelInfoList[0];
                    }
                    else
                    {
                        return null;
                    }
                }
            }
            else
            {
                return null;
            }
        }
    }
}
